package org.example.SolutionNew003;

import java.util.LinkedList;
import java.util.Queue;

public class DoubleTree002 {
    public static void main(String[] args) {
        DoubleTree002 dt = new DoubleTree002();
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        System.out.println(dt.maxDepth(root));
    }

    //深度
    public int maxDepth1(TreeNode root) {
        if(root==null) return 0;
        int leftDepth = maxDepth1(root.left);
        int rightDepth = maxDepth1(root.right);
        return Math.max(leftDepth, rightDepth) + 1;
    }

    //广度/层序
    public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        int depth=0;
        while(!queue.isEmpty()){
            int size=queue.size();
            while(size>0){
                TreeNode node=queue.poll();
                if(node.left!=null) queue.add(node.left);
                if(node.right!=null) queue.add(node.right);
                size--;
            }
            depth++;
        }
        return depth;
    }
}
